package org.tity.test.java.concurrent.threadlocal;

public class ThreadLocalMain {
	
	private static final ThreadLocal<Object> threadLocal = new ThreadLocal<Object>();
    
    
    public static void main(String[] args)
    {
//        new Thread(new MyIntegerTask("IntegerTask1")).start();
        new Thread(new MyStringTask("StringTask1","1")).start();
//        new Thread(new MyIntegerTask("IntegerTask2")).start();
        new Thread(new MyStringTask("StringTask2","2")).start();
    }
    
    
    public static class MyIntegerTask implements Runnable
    {
        private String name;
         
        MyIntegerTask(String name)
        {
            this.name = name;
        }
 
        @Override
        public void run()
        {
            for(int i = 0; i < 5; i++)
            {
                // ThreadLocal.get方法获取线程变量
                if(null == ThreadLocalMain.threadLocal.get())
                {
                    // ThreadLocal.et方法设置线程变量
                	ThreadLocalMain.threadLocal.set(0);
                    System.out.println("线程" + name + ": 0");
                }
                else
                {
                    int num = (Integer)ThreadLocalMain.threadLocal.get();
                    ThreadLocalMain.threadLocal.set(num + 1);
                    System.out.println("线程" + name + ": " + ThreadLocalMain.threadLocal.get());
                    if(i == 3)
                    {
                        ThreadLocalMain.threadLocal.remove();
                    }
                }
                try
                {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }  
        }
         
    }
     
    public static class MyStringTask implements Runnable
    {
        private String name;
        private String seed;
         
        MyStringTask(String name,String seed)
        {
            this.name = name;
            this.seed=seed;
        }
 
        @Override
        public void run()
        {
            for(int i = 0; i < 5; i++)
            {
                if(null == ThreadLocalMain.threadLocal.get())
                {
                    ThreadLocalMain.threadLocal.set(name+":");
//                    System.out.println("线程" + name + ": a");
                }
                else
                {
                    String str = (String)ThreadLocalMain.threadLocal.get();
                    ThreadLocalMain.threadLocal.set(str + "-"+seed);
                    System.out.println("线程" + name + ": " + ThreadLocalMain.threadLocal.get());
                }
                try
                {
                    Thread.sleep(800);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }
         
    }

}
